#!/usr/bin/env python
# encoding: utf-8
"""
piece.py

Created by Morgan Quirk on 2007-07-04.
"""

class Piece:    
    rotations=[]
    
    def __init__(self, pos=(3,0), fallrate=10, fallblocks=1, enterdelay=10, lockdelay=10):
        self.rotation=0     #Which position is the piece in?
        self.pos=pos
        self.x, self.y=pos[0]+self.enter_pos[0],pos[1]+self.enter_pos[1]-2
        self.enterdelay=enterdelay
        self.fallrate=fallrate  #How many ticks between drops
        self.fallblocks=fallblocks  #How many blocks to drop each time
        self.lockdelay=lockdelay    #Time it can be on the ground before it is locked
        self.ticks=0
        self.locktimer=None
        self.falling=0
        self.fallnow=0
        
    def tick(self, board):
        self.ticks+=1        
        self.blocks=self.rotations[self.rotation]
        if self.ticks>self.enterdelay and self.falling==0:
            self.x=self.pos[0]+self.enter_pos[0]
            self.y=self.pos[1]+self.enter_pos[1]-1
            self.falling=1            
        if self.falling:
            if self.ticks%self.fallrate==0 and self.falling or self.fallnow>0 and self.falling:
                i=0
                ground=0
                #Instead of moving it then checking, move and check every space we pass by
                if self.fallnow>0:
                    fallnum=self.fallnow
                else:
                    fallnum=self.fallblocks
                while i<fallnum and ground==0:
                    i+=1
                    ground=self.test_collision( [(x+self.x,y+self.y+1) for (x,y) in self.rotations[self.rotation]], board)
                    if ground==1:
                        if self.locktimer is None:
                            self.locktimer=self.lockdelay
                    else:
                        self.y+=1
                self.fallnow=0
                    
            if self.locktimer is not None and self.locktimer>0:                    
                if not self.test_collision( [(x+self.x,y+self.y+1) for (x,y) in self.rotations[self.rotation]], board):
                    self.locktimer=self.lockdelay
                    
            if self.locktimer==0 and self.test_collision( [(x+self.x,y+self.y+1) for (x,y) in self.rotations[self.rotation]], board):
                return True
            elif self.locktimer>0:
                self.locktimer-=1
            return False
                            
    def test_collision(self, spaces, board):
        for (bx,by) in spaces:
            if bx<0 or bx>=board.w or by>=board.h or board.grid[bx][by]==1:
                return True
        return False
        
    def rotate(self, direction, board):
        if direction==1 and self.rotation==3:
            r=0
        elif direction==-1 and self.rotation==0:
            r=3
        else:
            r=self.rotation+direction
        if not self.test_collision( [(x+self.x,y+self.y) for (x,y) in self.rotations[r]], board):
            self.rotation=r
        elif not self.test_collision( [(x+self.x+1,y+self.y) for (x,y) in self.rotations[r]], board):
            self.rotation=r
            self.x+=1            
        elif not self.test_collision( [(x+self.x-1,y+self.y) for (x,y) in self.rotations[r]], board):
            self.rotation=r
            self.x-=1
        else:
            return
            
    def shift(self, direction, board):
        if self.falling:
            if not self.test_collision( [(x+self.x+direction,y+self.y) for (x,y) in self.rotations[self.rotation]], board):
                self.x+=direction
                
        
        

class Piece_Line(Piece):
    rotations=  [
        [(0,1), (1,1), (2,1), (3,1)],
        [(2,0), (2,1), (2,2), (2,3)],
        [(0,1), (1,1), (2,1), (3,1)],
        [(2,0), (2,1), (2,2), (2,3)],        
                ]
    enter_pos=(0,-1)
    color=(200,60,0)                
                
class Piece_T(Piece):
    rotations=  [
        [(0,1), (1,1), (1,2), (2,1)],
        [(0,1), (1,0), (1,1), (1,2)],
        [(0,2), (1,1), (1,2), (2,2)],
        [(1,0), (1,1), (1,2), (2,1)],
                ]

    enter_pos=(0,-1)                
    color=(0,200,200)                
                
class Piece_L(Piece):
    rotations=  [
        [(0,1), (0,2), (1,1), (2,1)],
        [(0,0), (1,0), (1,1), (1,2)],
        [(0,2), (1,2), (2,1), (2,2)],
        [(1,0), (1,1), (1,2), (2,2)],
                ]

    enter_pos=(0,-1)                                
    color=(230,160,0)                
                
class Piece_J(Piece):
    rotations=  [
        [(0,1), (1,1), (2,1), (2,2)],
        [(1,0), (1,1), (1,2), (0,2)],
        [(0,1), (0,2), (1,2), (2,2)],
        [(1,1), (1,0), (1,2), (2,0)],
                ]
                
    enter_pos=(0,-1)                                
    color=(0,20,200)                
                
class Piece_S(Piece):
    rotations=  [
        [(0,2), (1,1), (1,2), (2,1)],
        [(0,0), (0,1), (1,1), (1,2)],
        [(0,2), (1,1), (1,2), (2,1)],
        [(0,0), (0,1), (1,1), (1,2)],
                ]

    enter_pos=(0,-1)                
    color=(180,0,200)

class Piece_Z(Piece):
    rotations=  [
        [(0,1), (1,1), (1,2), (2,2)],
        [(2,0), (2,1), (1,1), (1,2)],
        [(0,1), (1,1), (1,2), (2,2)],
        [(2,0), (2,1), (1,1), (1,2)],        
                ]

    enter_pos=(0,-1)                
    color=(0,200,10)
                
class Piece_Square(Piece):
    rotations=  [
        [(0,0), (0,1), (1,1), (1,0)],
        [(0,0), (0,1), (1,1), (1,0)],
        [(0,0), (0,1), (1,1), (1,0)],
        [(0,0), (0,1), (1,1), (1,0)],
                ]
                
    enter_pos=(1,0)            
    color=(200,200,0)